<template>
	<view style="padding-bottom: 100px;">
		<view class="wu" v-if="order_dianzilist_data.length==0">
			<image src="../../static/wu.png" style="width: 182px; height: 150px; " mode=""></image>
			<p>暂无数据</p>
		</view>
		<view class="dd" v-for="(time,index) in order_dianzilist_data">
			<view class="dd-top">
				<view class="dd-q">
					{{time.tms_order_sn}}
				</view>
				<!-- 已通过  申请中  已拒绝 -->
				<!-- 申请状态:1=审核中,2=已拒绝,3=已打款 status -->
				<!-- :style="
					time.is_advance==0?'color:#1890FF;':
					time.is_advance==1?'color:#0ACF00;':''
					" -->
				<view class="dd-p">
					<!-- 					{{
							time.is_advance==0?'未垫子':
							time.is_advance==1?'已垫子':''
						}} -->
					<!-- 正常使用 -->
					<!-- v-model="true" -->

					是否配送：<zero-switch v-model="order_dianzilist_data[index].is_delivery" :showText="true" textOn="是"
						textOff="否" size="22"></zero-switch>
				</view>
			</view>
			<view class="dd-dd" @click="tzxx(time.id)">
				<p>应垫资金额：{{time.should_advance_amount}}</p>
				<p>配送费：{{time.delivery_amount}}</p>
				<p>大车费：{{time.freight_amount}}</p>
				<p>实际垫资金额：{{time.advance_amount}}</p>
				<p>地址：{{time.address}}</p>
			</view>

		</view>

		<view class="kk" v-if="need_amount">
			配送费：
			<input type="digit" v-model="amount" placeholder="请输入配送费用">
		</view>

		<button @click="baocun">保存</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				order_dianzilist_data: {},
				waybill_id: "",
				amount: '',
				need_amount: true, //是否要配送费
			}
		},
		onLoad(open) {
			let data = JSON.parse(open.data);
			this.waybill_id = data[0].waybill_id;
			this.get_delivery_list();
		},
		methods: {
			baocun() {
				//设置不配送
				for (let i = 0; i < this.order_dianzilist_data.length; i++) {
					if (this.order_dianzilist_data[i].is_delivery == false) {
						this.order_noDelivery(this.order_dianzilist_data[i].id);
					}
				}

				if (this.amount == '' && this.need_amount) {
					uni.showToast({ title: "请输入配送费用", icon: 'none' })
					return;
				}

				// 提交金额
				// order_save_delivery_amount
				let data = {
					id: this.waybill_id,
					amount: this.amount || 0
				}
				//发车
				this.$rqt.order_save_delivery_amount(data).then(res => {
					let datad = { id: this.waybill_id }
					this.$rqt.waybill_fache(datad).then(res => {
						console.log(res);
						uni.showToast({ title: res.msg, icon: "success", });

						setTimeout(() => {
							uni.navigateBack({
								delta: 1, //返回层数，2则上上页
							})
						}, 850);
					})
				})
			},
			tzxx(x) {
				uni.navigateTo({
					url: "/pages/my/c_yunshudianzixiangqing?id=" + x
				});
			},
			//订单确认不配送
			order_noDelivery(id) {
				let data = {
					order_id: id
				}
				this.$rqt.order_noDelivery(data).then(res => {})
			},
			//运费垫资列表
			order_dianzilist() {
				let data = {
					waybill_id: this.waybill_id
				}
				this.$rqt.orderlists(data).then(res => {
					let list = [];
					for (let i = 0; i < res.data.data.length; i++) {
						// 添加开关
						list.push({
							...res.data.data[i],
							kg: true
						})
					}
					this.order_dianzilist_data = list;
					console.log(list);
				})
			},
			//需要配送的订单
			get_delivery_list() {
				let data = {
					id: this.waybill_id
				}
				this.$rqt.get_delivery_list(data).then(res => {
					console.log("res: ", res);
					this.order_dianzilist_data = res.data || [];
				})
			},
			// 处理是否要输入配送费
			deliveryFee() {
				let map_t = this.order_dianzilist_data || [];

				// 需要配送数量
				let need_delivery_num = 0;
				map_t.forEach((value, index, array) => {
					let is_delivery = value.is_delivery;
					if (is_delivery) {
						need_delivery_num++;
					}
				})
				// 需要配送数量大于0
				if (need_delivery_num > 0) {
					this.need_amount = true
				} else {
					this.need_amount = false
				}
			}

		},
		watch: {
			amount(val, oval) {

			},
			order_dianzilist_data: {
				handler(val, oval) {
					this.deliveryFee();
				},
				deep: true
			}
		},
	}
</script>

<style>
	.kk {
		width: 90%;
		margin: 0 auto;
		padding-top: 10px;
		display: flex;
		align-items: center;
	}

	.kk>input {}

	button {
		margin-top: 30px;
		width: 90%;
		height: 50px;
		background: #1890FF;
		box-shadow: 0px 0px 4px 0px rgba(204, 204, 204, 0.5);
		border-radius: 25px;
		font-size: 18px;
		font-weight: 600;
		color: #FFFFFF;
	}

	.wu {
		width: 100%;
		text-align: center;
		padding-top: 60px;
		color: #999999;
		font-size: 18px;
		line-height: 40px;
	}

	.dd {
		width: 100%;
		padding-bottom: 10px;
		margin-bottom: 10px;
		background: #FFFFFF;
		box-shadow: 0px 0px 6px 0px rgba(204, 204, 204, 0.5);
	}

	.dd-top {
		width: 100%;
		display: flex;
		justify-content: space-between;
		border-bottom: 1px dashed #CCCCCC;
	}

	.dd-q {
		font-size: 18px;
		font-weight: 500;
		margin-left: 20px;
		line-height: 50px;
	}

	.dd-p {
		font-size: 28rpx;
		line-height: 50px;
		margin-right: 20px;
		color: #1890FF;
		/* font-weight: 600; */
		display: flex;
		align-items: center;
	}

	.dd-dd {
		overflow: hidden;
		width: 90%;
		margin: 0 auto;
	}

	.dd-dd>p {
		line-height: 40px;
		float: left;
		width: 50%;
		font-size: 16px;
		font-weight: 400;
	}
</style>